<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">

    var lonlat; 

    function test_LonLat_constructor (t) {
        t.plan( 8 );
        lonlat = new OpenLayers.LonLat(6, 5);
        t.ok( lonlat instanceof OpenLayers.LonLat, "new OpenLayers.LonLat returns LonLat object" );
        t.eq( lonlat.CLASS_NAME, "OpenLayers.LonLat", "lonlat.CLASS_NAME is set correctly");
        t.eq( lonlat.lon, 6, "lonlat.lon is set correctly");
        t.eq( lonlat.lat, 5, "lonlat.lat is set correctly");

        // possible global Mercator projection values
        lonlat = new OpenLayers.LonLat(20037508.33999999, -20037508.33999999);
        t.eq( lonlat.lon, 20037508.34, "lonlat.lon rounds correctly");
        t.eq( lonlat.lat, -20037508.34, "lonlat.lat rounds correctly");
        
        // allow construction from single arg
        lonlat = new OpenLayers.LonLat([1, 2]);
        t.eq(lonlat.lon, 1, "lon from array");
        t.eq(lonlat.lat, 2, "lat from array");
        
    }
    
    function test_LonLat_constructorFromStrings (t) {
        t.plan( 4 );
        lonlat = new OpenLayers.LonLat("6", "5");
        t.ok( lonlat instanceof OpenLayers.LonLat, "new OpenLayers.LonLat returns LonLat object" );
        t.eq( lonlat.CLASS_NAME, "OpenLayers.LonLat", "lonlat.CLASS_NAME is set correctly");
        t.eq( lonlat.lon, 6, "lonlat.lon is set correctly");
        t.eq( lonlat.lat, 5, "lonlat.lat is set correctly");
    }

    function test_LonLat_toString(t) {
        t.plan( 1 );
        lonlat = new OpenLayers.LonLat(5,6);
        t.eq( lonlat.toString(), "lon=5,lat=6", "lonlat.toString() returns correctly");
    }

    function test_LonLat_toShortString(t) {
        t.plan( 1 );
        lonlat = new OpenLayers.LonLat(5,6);
        t.eq( lonlat.toShortString(), "5, 6", "lonlat.toShortString() returns correctly");
    }

    function test_LonLat_clone(t) {
        t.plan( 3 );
        oldLonLat = new OpenLayers.LonLat(5,6);
        lonlat = oldLonLat.clone();
        t.ok( lonlat instanceof OpenLayers.LonLat, "clone returns new OpenLayers.LonLat object" );
        t.ok( lonlat.equals(oldLonLat), "lonlat is set correctly");
        
        oldLonLat.lon = 100;
        t.eq( lonlat.lon, 5, "changing oldLonLat.lon doesn't change lonlat.lon");
    }

    function test_LonLat_add(t) {
        t.plan(8);

        origLL = new OpenLayers.LonLat(10,100);
        lonlatA = origLL.clone();

        addpx = lonlatA.add(5, 50);
        t.ok( lonlatA.equals(origLL), "lonlatA is not modified by add operation");

        var ll = new OpenLayers.LonLat(15,150);
        t.ok( addpx.equals(ll), "addpx is set correctly");
        
    //null values
        try {
            addpx = lonlatA.add(null, 50);
        } catch(e) {
            t.ok("exception thrown when passing null value to add()");
        }
        t.ok( lonlatA.equals(origLL), "lonlatA is not modified by erroneous add operation (null lon)");
 
        try {
            addpx = lonlatA.add(5, null);
        } catch(e) {
            t.ok("exception thrown when passing null value to add()");
        }
        t.ok( lonlatA.equals(origLL), "lonlatA is not modified by erroneous add operation (null lat)");

        // string values
        addpx = origLL.clone().add("5", "50");
        t.eq(addpx.lon, 15, "addpx.lon is set correctly");
        t.eq(addpx.lat, 150, "addpx.lat is set correctly");
    }
    
    function test_LonLat_equals(t) {
        t.plan( 5 );
        lonlat = new OpenLayers.LonLat(5,6);

        ll = new OpenLayers.LonLat(5,6);
        t.eq( lonlat.equals(ll), true, "(5,6) equals (5,6)");

        ll = new OpenLayers.LonLat(1,6);
        t.eq( lonlat.equals(ll), false, "(5,6) does not equal (1,6)");

        ll = new OpenLayers.LonLat(5,2);
        t.eq( lonlat.equals(ll), false, "(5,6) does not equal (5,2)");

        ll = new OpenLayers.LonLat(1,2);
        t.eq( lonlat.equals(ll), false, "(5,6) does not equal (1,2)");

        t.ok( !lonlat.equals(null), "equals() returns false on comparison to null");

    }

    function test_LonLat_fromString(t) {
        t.plan( 2 );
        lonlat = OpenLayers.LonLat.fromString("6,5");
        t.ok( lonlat instanceof OpenLayers.LonLat, "OpenLayers.LonLat.fromString() returns LonLat object" );

        var ll = new OpenLayers.LonLat(6, 5);
        t.ok( lonlat.equals(ll), "lonlat is set correctly");
    }
    
    function test_LonLat_fromArray(t) {
        t.plan( 3 );
        
        // (1 test) must return a OpenLayers.LonLat-instance 
        lonlat = OpenLayers.LonLat.fromArray([6,5]);
        t.ok( lonlat instanceof OpenLayers.LonLat, "OpenLayers.LonLat.fromArray returns LonLat object" );

        var ll = new OpenLayers.LonLat(6, 5);
        // (1 test) must return correct LonLat-object
        t.ok( lonlat.equals(ll), "lonlat is set correctly");
        
        
        // (1 test) check how function deals with illegal arguments, it should 
        // never throw an exception and always return an instance of 
        // OpenLayers.LonLat.
        var unexpectedResult = false,
            undef,
            checkArgs = [
                {},
                '',
                6,
                false,
                true,
                [undef, 5],
                [6, undef]
            ],
            returnedVal;
            
        try {
            for(var i = 0, len = checkArgs.length; i < len; i++ ){
                returnedVal = OpenLayers.LonLat.fromArray( checkArgs[i] );
                if (!(returnedVal instanceof OpenLayers.LonLat) ) {
                    unexpectedResult = true;
                    break;
                }
            }
            // no arguments at all
            returnedVal = OpenLayers.LonLat.fromArray();
            unexpectedResult = !(returnedVal instanceof OpenLayers.LonLat);
        } catch(e) {
            unexpectedResult = true;
        } finally {
            t.ok(!unexpectedResult, "OpenLayers.LonLat.fromArray always returns an instance of OpenLayers.LonLat and doesn't throw an exception when called with unexpected argument.");
        }
    }

    function test_LonLat_transform(t) {
        t.plan(10);
        lonlat = new OpenLayers.LonLat(10, -10);
        lonlat.transform(new OpenLayers.Projection("foo"), new OpenLayers.Projection("Bar")); 
        t.eq(lonlat.lon, 10, "lon for null transform is the same")
        t.eq(lonlat.lat, -10, "lat for null transform is the same")
        lonlat.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); 
        t.eq(Math.round(lonlat.lon), 1113195, "lon for spherical mercator transform is correct");
        t.eq(Math.round(lonlat.lat), -1118890, "lat for spherical mercator correct")
        lonlat.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326")); 
        t.eq(lonlat.lon, 10, "lon for inverse spherical mercator transform is correct");
        t.eq(Math.round(lonlat.lat), -10, "lat for inverse spherical mercator correct")
        
        // transform with string
        lonlat = new OpenLayers.LonLat(10, -10);
        lonlat.transform("EPSG:4326", "EPSG:900913"); 
        t.eq(Math.round(lonlat.lon), 1113195, "(string) lon for spherical mercator transform is correct");
        t.eq(Math.round(lonlat.lat), -1118890, "(string) lat for spherical mercator correct")
        lonlat.transform("EPSG:900913", "EPSG:4326"); 
        t.eq(lonlat.lon, 10, "(string) lon for inverse spherical mercator transform is correct");
        t.eq(Math.round(lonlat.lat), -10, "(string) lat for inverse spherical mercator correct")
        
    }
    
    function test_LonLat_wrapDateLine(t) {
        t.plan( 6 );

        var goodLL = new OpenLayers.LonLat(0,0);
        var testLL, wrappedLL;

  //bad maxextent
        var maxExtent = null;

        testLL = goodLL.clone();
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll with a bad maxextent does nothing");
        
        
  //good maxextent
        maxExtent = new OpenLayers.Bounds(-10,-10,10,10);

    //inside
        testLL = goodLL.clone();
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll within the maxextent does nothing");
        
    //left
        testLL = goodLL.add(-20,0);
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll once left of maxextent works");
        
    //way left
        testLL = goodLL.add(-200,0);
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll way left of maxextent works");

    //right
        testLL = goodLL.add(20,0);
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll once right of maxextent works");
        
    //way right
        testLL = goodLL.add(200,0);
        wrappedLL = testLL.wrapDateLine(maxExtent);
        t.ok(wrappedLL.equals(goodLL), "wrapping a ll way right of maxextent works");

    }


  </script>
</head>
<body>
</body>
</html>
